<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="cn">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />






















<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=6.0.0" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=6.0.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=6.0.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=6.0.0">


  <link rel="mask-icon" href="/images/logo.svg?v=6.0.0" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta property="og:type" content="website">
<meta property="og:title" content="categories">
<meta property="og:url" content="http://yoursite.com/categories/index.html">
<meta property="og:site_name" content="hill&#39;s blog">
<meta property="og:locale" content="cn">
<meta property="article:published_time" content="2017-07-10T08:36:26.000Z">
<meta property="article:modified_time" content="2021-02-25T04:04:48.836Z">
<meta property="article:author" content="heyaojia">
<meta name="twitter:card" content="summary">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '6.0.0',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":false,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/categories/"/>





  <title>Makefile学习 | hill's blog</title>
  








<meta name="generator" content="Hexo 4.2.1"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="cn">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"> <div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">hill's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            Archives
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            Categories
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2021/05/07/%E5%B7%A5%E5%85%B7%E5%BA%94%E7%94%A8/MakeFile/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="heyaojia">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="hill's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Makefile学习</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2021-05-07T17:22:27+08:00">2021-05-07</time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%B7%A5%E5%85%B7%E5%BA%94%E7%94%A8/" itemprop="url" rel="index">
                    <span itemprop="name">工具应用</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="Makefile"><a href="#Makefile" class="headerlink" title="Makefile"></a>Makefile</h3><hr>
<p>目的：</p>
<ol>
<li>自己能写简单的Makefile。</li>
<li>看懂大神写的Makefile。</li>
</ol>
<ul>
<li>引用自:<a href="https://blog.csdn.net/weixin_38391755/article/details/80380786/" target="_blank" rel="noopener">https://blog.csdn.net/weixin_38391755/article/details/80380786/</a></li>
</ul>
<h4 id="1-Makefile-介绍"><a href="#1-Makefile-介绍" class="headerlink" title="1. Makefile 介绍"></a>1. Makefile 介绍</h4><p>​        一句话概括，<strong>Makefile</strong>作用就是来编译工程的，当然，在Linux系统中，Shell脚本也可以实现这样的功能， 目的是提高开发的效率。</p>
<a id="more"></a>


<h4 id="2-Makefile基本语法"><a href="#2-Makefile基本语法" class="headerlink" title="2. Makefile基本语法"></a>2. Makefile基本语法</h4><h5 id="2-1-以下是一个基本的Makefile规则："><a href="#2-1-以下是一个基本的Makefile规则：" class="headerlink" title="2.1 以下是一个基本的Makefile规则："></a>2.1 以下是一个基本的Makefile规则：</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">target... : prerequisites ...</span><br><span class="line"></span><br><span class="line">command</span><br><span class="line"></span><br><span class="line">...</span><br></pre></td></tr></table></figure>

<ul>
<li>target: 就是目标文件，可以是执行文件<code>Object file</code>、或者是一个标签（label）</li>
<li>prerequisites:要生成target所需要的文件或者是目标</li>
<li>command也就是make需要执行的命令</li>
<li>这是一个文件的依赖关系，也就是说，target这一个或多个的目标文件依赖于prerequisites中的文件，其生成规则定义在command中。说白一点就是说，prerequisites中如果有一个以上的文件比target文件要新的话，command所定义的命令就会被执行。这就是Makefile的规则。也就是Makefile中最核心的内容。</li>
<li>另外：</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">###【注】：在看别人写的Makefile文件时，你可能会碰到以下三个变量：$@，$^，$&lt;代表的意义分别是： </span></span><br><span class="line"></span><br><span class="line"><span class="comment">###他们三个是十分重要的三个变量，所代表的含义分别是：</span></span><br><span class="line"></span><br><span class="line"><span class="comment">###$@--目标文件，$^--所有的依赖文件，$&lt;--第一个依赖文件。  反斜杠（\）是换行符的意思 -表示错误跳过</span></span><br></pre></td></tr></table></figure>



<h5 id="２-2-make是如何工作的"><a href="#２-2-make是如何工作的" class="headerlink" title="２.2 make是如何工作的"></a>２.2 <strong>make是如何工作的</strong></h5><ol>
<li>make执行目录下的Makefile 或者makefile</li>
<li>运行文件中的第一个target</li>
<li>如果target文件不存在或者require file 的修改时间比target修改时间新,就执行command</li>
<li>如果没问题,会检查target为require的目标</li>
</ol>
<h5 id="２-３-Makefile里主要包含了五个东西：变量定义、显式规则、隐晦规则、文件指示和注释-含义照搬了"><a href="#２-３-Makefile里主要包含了五个东西：变量定义、显式规则、隐晦规则、文件指示和注释-含义照搬了" class="headerlink" title="２.３ Makefile里主要包含了五个东西：变量定义、显式规则、隐晦规则、文件指示和注释(含义照搬了)"></a>２.３ Makefile里主要包含了五个东西：变量定义、显式规则、隐晦规则、文件指示和注释(含义照搬了)</h5><ol>
<li>显式规则。显式规则说明了，如何生成一个或多的的目标文件。这是由Makefile的书写者明显指出，要生成的文件，文件的依赖文件，生成的命令。</li>
<li>隐晦规则。由于我们的make有自动推导的功能，所以隐晦的规则可以让我们比较粗糙地简略地书写Makefile，这是由make所支持的。</li>
<li>变量的定义。在Makefile中我们要定义一系列的变量，变量一般都是字符串，这个有点你C语言中的宏，当Makefile被执行时，其中的变量都会被扩展到相应的引用位置上。</li>
<li>文件指示。其包括了三个部分，一个是在一个Makefile中引用另一个Makefile，就像C语言中的include一样；另一个是指根据某些情况指定Makefile中的有效部分，就像C语言中的预编译#if一样；还有就是定义一个多行的命令。有关这一部分的内容，我会在后续的部分中讲述。</li>
<li>注释。Makefile中只有行注释，和UNIX的Shell脚本一样，其注释是用“#”字符，这个就像C/C++中的“//”一样。如果你要在你的Makefile中使用“#”字符，可以用反斜框进行转义，如：“#”。</li>
</ol>
<h5 id="2-4-Makefile一些要点"><a href="#2-4-Makefile一些要点" class="headerlink" title="2.4 Makefile一些要点:"></a>2.4 Makefile一些要点:</h5><ul>
<li><p>让make自动推导</p>
<ul>
<li>只要make看到一个[.o]文件，它就会自动的把[.c]文件加在依赖关系中，如果make找到一个whatever.o，那么whatever.c，就会是whatever.o的依赖文件。并且 cc -c whatever.c 也会被推导出来。</li>
</ul>
</li>
<li><p>Makefile的文件名:</p>
<ul>
<li>一般情况下会默认寻找<code>GNUMakefile</code>、<code>makefile</code>、<code>Makefile</code>,默认用Makefile,一目了然。</li>
</ul>
</li>
<li><p>引用其他的<strong>Makefile</strong>:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">&#96;&#96;&#96;bash</span><br><span class="line">### make命令开始时，会把找寻include所指出的其它Makefile，并把其内容安置在当前的位置。</span><br><span class="line">1.如果make执行时，有“-I”或“--include-dir”参数，那么make就会在这个参数所指定的目录下去寻找。</span><br><span class="line">2.如果目录&#x2F;include（一般是：&#x2F;usr&#x2F;local&#x2F;bin或&#x2F;usr&#x2F;include）存在的话，make也会去找。</span><br></pre></td></tr></table></figure>



</li>
</ul>
<h5 id="2-5-Makefile基本书写规则"><a href="#2-5-Makefile基本书写规则" class="headerlink" title="2.5 Makefile基本书写规则:"></a>2.5 Makefile基本书写规则:</h5><ul>
<li><p>通配符：</p>
<p>跟Linux Shell一致，</p>
<figure class="highlight plain"><figcaption><span>``` :home目录</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">  &#96;&#96;&#96;* &#96;&#96;&#96;: 通用</span><br><span class="line"></span><br><span class="line">* 伪目标</span><br><span class="line"></span><br><span class="line">  &#96;&#96;&#96;makefile</span><br><span class="line">  .PHONY : clean</span><br><span class="line">  </span><br><span class="line">  pre_clean:</span><br><span class="line">  	@echo &quot;pre clean&quot;</span><br><span class="line">  </span><br><span class="line">  clean: pre_clean</span><br><span class="line">  	rm *.o temp</span><br></pre></td></tr></table></figure>

<ul>
<li>意在不论target存不存在， 只要make显示指明了这个target，都要执行</li>
<li>并且这个target还可以用依赖，类似函数调用</li>
</ul>
</li>
<li><p>变量:</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">variable := <span class="string">"test"</span></span><br><span class="line"></span><br><span class="line"><span class="section">show:</span></span><br><span class="line"> @echo $&#123;variable&#125;</span><br></pre></td></tr></table></figure>




</li>
</ul>
<ul>
<li>小结：<ul>
<li>MakeFIle的功能比较强大，很多语法未必用得到，只能在使用中总结</li>
</ul>
</li>
</ul>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kity@2.0.4/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kityminder-core@1.4.50/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.css">
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2021/04/21/Nosql/Redis-%E8%BF%9B%E9%98%B6/" rel="next" title="Nosql/Redis-进阶">
                <i class="fa fa-chevron-left"></i> Nosql/Redis-进阶
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2021/05/08/Mysql/MYSQL-%E7%B4%A2%E5%BC%95/" rel="prev" title="Mysql/MYSQL-索引">
                Mysql/MYSQL-索引 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">heyaojia</p>
              <p class="site-description motion-element" itemprop="description">游戏行业的一枚小鲜肉</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives">
              
                  <span class="site-state-item-count">21</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">5</span>
                  <span class="site-state-item-name">categories</span>
                </a>
              </div>
            

            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="git@gitee.com:hillcool/hillcool.git" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-gitee"></i>GitHub</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#Makefile"><span class="nav-number">1.</span> <span class="nav-text">Makefile</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-Makefile-介绍"><span class="nav-number">1.1.</span> <span class="nav-text">1. Makefile 介绍</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-Makefile基本语法"><span class="nav-number">1.2.</span> <span class="nav-text">2. Makefile基本语法</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#2-1-以下是一个基本的Makefile规则："><span class="nav-number">1.2.1.</span> <span class="nav-text">2.1 以下是一个基本的Makefile规则：</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#２-2-make是如何工作的"><span class="nav-number">1.2.2.</span> <span class="nav-text">２.2 make是如何工作的</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#２-３-Makefile里主要包含了五个东西：变量定义、显式规则、隐晦规则、文件指示和注释-含义照搬了"><span class="nav-number">1.2.3.</span> <span class="nav-text">２.３ Makefile里主要包含了五个东西：变量定义、显式规则、隐晦规则、文件指示和注释(含义照搬了)</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#2-4-Makefile一些要点"><span class="nav-number">1.2.4.</span> <span class="nav-text">2.4 Makefile一些要点:</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#2-5-Makefile基本书写规则"><span class="nav-number">1.2.5.</span> <span class="nav-text">2.5 Makefile基本书写规则:</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">heyaojia</span>

  

  
</div>









        







        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>
























  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=6.0.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=6.0.0"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=6.0.0"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=6.0.0"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=6.0.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=6.0.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=6.0.0"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
